

package top.continew.admin.question.user.service.question.entity;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Data;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.model.question.QuestionKnowledgePoint;

import java.util.List;

@Data
public abstract class AbstractQuestion {
    //题目id
    protected Long id;
    //材料
    private String material;
    //题目
    private String title;
    // 难度
    private Long difficultyLevel;
    // 解析
    private String analysis;
    // 知识点
    private List<String> knowledgePoints;
    // 来源
    private String source;
    // 父id
    private Long parentId;
    private Long specificationType;

    protected String answer;
    protected Double score;

    public void init(Question question) {
        this.id = question.getId();
        this.material = question.getMaterials();
        this.title = question.getTitle();
        this.difficultyLevel = question.getDifficultyLevel();
        this.analysis = question.getAnalyze();
        this.source = question.getOfficialQuestionSource();
        this.answer = question.getAnswer();
        this.score = question.getScore();
        this.specificationType = question.getSpecificationType();
        this.parentId = question.getParentId();
        if (question.getParentId() != 0) {
            Question parentQuestion = new Question().selectById(question.getParentId());
            if (parentQuestion != null) {
                this.material = parentQuestion.getMaterials();
            }
        }
    }

    public String getAnswer() {
        String answerJson = this.answer;
        List<String> array = JSON.parseArray(answerJson, String.class);
        return String.join(",", array);
    }

    public void init(Long questionId) {
        Question question = new Question().selectById(questionId);
        init(question);
        this.knowledgePoints = initKnowledgePoints();
    }

    public List<String> initKnowledgePoints() {
        List<QuestionKnowledgePoint> questionKnowledgePoint = new QuestionKnowledgePoint().selectList(Wrappers
            .lambdaQuery(QuestionKnowledgePoint.class)
            .eq(QuestionKnowledgePoint::getQuestionId, this.id));
        if (questionKnowledgePoint.isEmpty()) {
            return List.of();
        }
        this.knowledgePoints = questionKnowledgePoint.stream().map(QuestionKnowledgePoint::getName).toList();
        return this.knowledgePoints;
    }

    public abstract CorrectResult correct(String userAnswer);

    public abstract Integer getType();

    public abstract String getTypeName();

    @Data
    public static class CorrectResult {
        // 正确答案
        private String correctAnswer;
        // 用户答案
        private String userAnswer;
        // 是否正确
        private Integer problemState;
    }

}